import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 随机读写类
 * @author near
 */
public class RandomIO {
    /**
     * 写入文件
     * @param s
     * @param oraf
     * @throws IOException
     */
    public void writeString(String s, OptimizedRandomAccessFile oraf) throws IOException {
        oraf.write(s.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 搜索关键字指定索引(关键字前、后位置)
     * @param randomObj
     * @param oraf
     * @return Map<String,Long> START_POS END_POS
     * @throws IOException
     */
    public Map<String,Long> searchPos(IRandomObj randomObj, OptimizedRandomAccessFile oraf) throws Exception {
        String keyWord = randomObj.getKeyWord();
        String currentLine;
        long startPos = 0;
        long endPos = -1;
        long replaceEndPos = -1;
        Map<String,Long> result = null;
        Enum matchPattern = randomObj.getMatchPattern();
        String finishSign = randomObj.getFinishSign();
        boolean equals = MatchPatternEnum.EQUALS.equals(matchPattern);
        boolean contains = MatchPatternEnum.CONTAIN.equals(matchPattern);
        boolean regex = MatchPatternEnum.REGEX.equals(matchPattern);
        boolean startsWith = MatchPatternEnum.STARTS_WITH.equals(matchPattern);
        boolean findFlag = false;
        while( (currentLine = oraf.readLine()) != null ){
            currentLine = new String(currentLine.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            endPos = oraf.getFilePointer();
            if( regex && currentLine.matches(keyWord) || equals && currentLine.equals(keyWord)
                    || contains && currentLine.contains(keyWord)  || startsWith && currentLine.startsWith(keyWord) ){
                findFlag = true;
                break;
            }
            startPos = oraf.getFilePointer();
        }
        if( findFlag ){
            result = new HashMap<>();
            result.put(Constants.START_POS,startPos);
            result.put(Constants.END_POS,endPos);
        }

        if(findFlag && StringUtils.isNotEmpty(finishSign) ){
            replaceEndPos = endPos;
            while( (currentLine = oraf.readLine()) != null ){
                currentLine = new String(currentLine.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
                if( currentLine.contains(finishSign) ){
                    break;
                }
                replaceEndPos = oraf.getFilePointer();
            }
            result.put(Constants.REPLACE_END_POS,replaceEndPos);
        }

        return result;

    }

    /**
     * 保存暂存数据
     * @param oraf
     * @return
     * @throws IOException
     */
    public StringBuilder getHoldContent(OptimizedRandomAccessFile oraf,long holdStartPos) throws IOException {
        oraf.seek(holdStartPos);
        String currentLine;
        StringBuilder holdContent = new StringBuilder();
        ;
        while( (currentLine = oraf.readLine()) != null ){
            currentLine = new String(currentLine.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            holdContent.append(currentLine).append("\r\n");
        }
        return holdContent;
    }

}
